<?php
/**
 * Dao基类
 * @author Administrator
 *
 */
class DaoD {
	protected $_m = array ();
	public $allowFields=array();
	public function __construct(array $requestData = array()) {
		$this->setRequestData ( $requestData );
	}
	public function setRequestData(array $requestData) {
		foreach ( $requestData as $key => $val ) {
			if (! class_exists ( $key )) {
				throw new CHttpException ( 403, '指定的模型[' . $key . ']不存在' );
			}
			if (! is_array ( $val )) {
				throw new CHttpException ( 403, '指定的模型[' . $key . ']的值不合法' );
			}
			$this->_m [$key] = new $key ();
			$this->_m [$key]->setAttributes ( $val, false );
			$array = $this->_m [$key]->getAttributes ();
			$arrayDiff = array_diff_assoc ( $val, $array );
			foreach ( $arrayDiff as $_k => $_v ) {
				property_exists ( $this, $_k ) && $this->$_k = $_v;
			}
			unset ( $arrayDiff );
			unset ( $array );
			unset ( $requestData );
		}
	}
	public function __get($key) {
		if (isset ( $this->_m [$key] )) {
			return $this->_m [$key];
		
		//else if (class_exists ( $key )) {
		//$this->_m [$key] = new $key ();
		//return $this->_m [$key];
		} else
			throw new CHttpException ( 403, '指定的模型[' . $key . ']不存在' );
	}
	/**
	 * 将错误信息转换为字符串
	 * @param CActiveRecord $obj  错误检查对象
	 * @param string $sep 错误消息之间的分隔符
	 * @param string $defaultError 默认错误消息，当getErrors()取不到错误时，说明发生某种操作异常错误，可设置一个默认的错误消息
	 * @return string
	 */
	public static function getErrorsToString(CActiveRecord $obj, $sep = ', ', $defaultError = '异常错误') {
		$errors = $obj->getErrors ();
		$msg = array ();
		if (count ( $errors ) > 0) {
			foreach ( $errors as $key => $err ) {
				if (! is_array ( $err ) || count ( $err ) < 1)
					continue;
				foreach ( $err as $_v ) {
					$msg [] = UtilD::deSlashes($_v);
				}
			}
		}
		if (count ( $msg ) < 1)
			$msg [] = $defaultError;
		return implode ( $sep, $msg );
	}
}
?>